<?php

class jadwal extends CI_Controller {

    function __construct() {
        parent::__construct();
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
        } else {
            redirect('user_login/user', 'refresh');
        }

//        $this->load->model('admin_bank_model');
        $this->load->model('rute');
        $this->load->model('departure');
        $this->load->model('airline');
        $this->load->model('user_role');
        $this->load->library('user_id');
        $this->load->helper('user_helper');
    }

    function index() {
        $data['id'] = $this->user_id->id();
        
        $data['i'] = 1;
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['rows'] = $this->airline->index();
        $data['content'] = ('jadwal/index');
        $this->load->view('template/user', $data);
    }

    function rute() {
        $data['id'] = $this->user_id->id();
        
        $data['id_maskapai'] = $this->uri->segment(3);
        $data['i'] = 1;
        $data['options'] = $this->rute->get_array();
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data["rows"] = $this->rute->index();
        $data['content'] = 'jadwal/rute';
        $this->load->view('template/user', $data);
    }

    function tanggal() {
        $data['id'] = $this->user_id->id();
        
        $plane = 1;
        $adult = 0;
        $infant = 0;
        $price = 0;
        if ($this->input->post('id_maskapai', TRUE)) {
            $id_maskapai = $this->input->post('id_maskapai', TRUE);
            $from = $this->input->post('from', TRUE);
            $to = $this->input->post('to', TRUE);
            $date = $this->input->post('date', TRUE);
            $data['jadwals'] = $this->departure->tanggal($id_maskapai, $from, $to, $date);
            $plane = $this->input->post('plane', TRUE);
            $adult = $this->input->post('adult', TRUE);
            $price = $this->input->post('price', TRUE);
            $infant = $this->input->post('infant', TRUE);
        } elseif ($this->uri->segment(3)) {
            $url = $this->uri->segment(3);
            $url = explode('-', $url);
            $id_maskapai = $url[0];
            $from = $url[1];
            $to = $url[2];
//            $date = $url[3].'-'.$url[4].'-'.$url[5];
            $plane = 1;
            $adult = 0;
            $price = 0;
//            $data['jadwals'] = $this->departure->tanggal($id_maskapai, $from, $to, $date);
            $data['jadwals'] = $this->departure->tanggal($id_maskapai, $from, $to);
        }
        $data['id_maskapai'] = $id_maskapai;
        $data['from'] = $from;
        $data['to'] = $to;
//        $data['date'] = $date;
        $data['jumlah'] = $plane;
        $data['plane'] = $plane;
        $data['adult'] = $adult;
        $data['infant'] = $infant;
        $data['price'] = $price;

        $data['i'] = 1;
        $data['options'] = $this->rute->get_array();
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data["rows"] = $this->rute->index();
        $data['content'] = 'jadwal/tanggal';
        $this->load->view('template/user', $data);
    }

    function date() {
        $url = $this->uri->segment(3);
        $id = explode('.', $url);
        $data['id_maskapai'] = $id[0];
        $data['id_rute'] = $id[1];
        $data['i'] = 1;
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data["rows"] = $this->rute->index();
        $data['content'] = 'jadwal/date';
        $this->load->view('template/user', $data);
    }

    function add() {
        $data['id'] = $this->user_id->id();
        
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['content'] = 'airlines/new';
        $this->load->view('template/user', $data);
    }

    function create() {
        $id_maskapai = $this->input->post('id_maskapai', TRUE);
//        $price = $this->input->post('price', TRUE);
        $flight = $this->input->post('flight', TRUE);
        $from = $this->input->post('from', TRUE);
        $to = $this->input->post('to', TRUE);
        $depart = $this->input->post('depart', TRUE);
        $arrives = $this->input->post('arrives', TRUE);
        $class = $this->input->post('class', TRUE);
        $adult = $this->input->post('adult', TRUE);
        $infant = $this->input->post('infant', TRUE);
        $date = $this->input->post('date', TRUE);

        $jumlah = count($id_maskapai);
        for ($i = 0; $i < $jumlah; $i++) {
//            $this->departure->create($id_maskapai[$i], $price[$i], $flight[$i], $from[$i], $to[$i], $depart[$i], $arrives[$i], $class[$i], $adult[$i], $infant[$i], $date[$i]);
            $this->departure->create($id_maskapai[$i], $flight[$i], $from[$i], $to[$i], $depart[$i], $arrives[$i]);
        }
//        redirect('jadwal/tanggal/' . $id_maskapai[0] . '-' . $from[0] . '-' . $to[0] . '-' . $date[0]);
        redirect('jadwal/tanggal/' . $id_maskapai[0] . '-' . $from[0] . '-' . $to[0]);
    }

//    function edit() {
//        $id = $this->uri->segment(3);
//
//        //cek
//        $access = $this->user_role->access($this->user_id->cek());
//        $this->user_role->chek($access, 'bank_edit');
//
//        $data['access'] = $access;
//        $data['rows'] = $this->airline->edit($id);
//        $data['content'] = 'airlines/edit';
//        $this->load->view('template/user', $data);
//    }

    function update() {
        $id = $this->input->post('id', TRUE);
        $id_maskapai = $this->input->post('id_maskapai', TRUE);
        $price = $this->input->post('price', TRUE);
        $flight = $this->input->post('flight', TRUE);
        $from = $this->input->post('from', TRUE);
        $to = $this->input->post('to', TRUE);
        $depart = $this->input->post('depart', TRUE);
        $arrives = $this->input->post('arrives', TRUE);
        $class = $this->input->post('class', TRUE);
        $adult = $this->input->post('adult', TRUE);
        $infant = $this->input->post('infant', TRUE);
        $date = $this->input->post('date', TRUE);

        $jumlah = count($id);
        for ($i = 0; $i < $jumlah; $i++) {
            $this->departure->update($id[$i], $id_maskapai[$i], $price[$i], $flight[$i], $from[$i], $to[$i], $depart[$i], $arrives[$i], $class[$i], $adult[$i], $infant[$i], $date[$i]);
        }

        redirect('jadwal/tanggal/' . $id_maskapai[0] . '-' . $from[0] . '-' . $to[0] . '-' . $date[0]);
    }

    function delete() {
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'bank_delete');
        $url = $this->uri->segment(3);
        $url = explode('-', $url);
        $id = $url[0];
        $id_maskapai = $url[1];
        $from = $url[2];
        $to = $url[3];
        $this->departure->delete($id, $id_maskapai, $from, $to);
    }

    function find() {
        
        $bandara_a = $this->input->post('bandara_a', TRUE);
        $bandara_t = $this->input->post('bandara_t', TRUE);
        $bandara_t = $this->input->post('bandara_t', TRUE);
        $bandara_t = explode(' ', $bandara_t);
        $count = count($bandara_t);

        if ($count == 2)
            $bandara_t = $bandara_t[0] . "%20" . $bandara_t[1];
        else {
            $bandara_t = $bandara_t[0];
        }
        $date = $this->input->post('date', TRUE);
        $date = explode('/', $date);
        $data['bandara_a'] = $bandara_a;
        $data['bandara_t'] = $bandara_t;
        $data['date'] = $date[1] . $date[0];
        $data['content'] = ('airlines/find');
        $this->load->view('template/airline', $data);
    }

}

